package com.theaf.basemodule.dao;

import com.theaf.basemodule.common.model.SysMenu;
import com.theaf.core.repo.base.BaseRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

/**
 * @Author:songwenke
 * @Description:菜单
 * @Date:create in 17:01 2018/1/3
 * @Modified By:
 **/
public interface MenuDAO extends BaseRepository<SysMenu,Long> {
    /**
     * 查询用户有权限的菜单
     * @param userId
     * @return
     */
    @Query(nativeQuery=true,value = "select t.* from sys_menu t where t.enable=true and\n" +
            "       EXISTS(select k.level_code like t.level_code||'%' from sys_menu k where \n" +
            "             EXISTS(select * from sys_resource a where a.enable=true and a.id=k.module_id and a.type=3)\n" +
            "             and (exists(select * from sys_pos_auth b,sys_user c where b.pos_id=c.pos_id and c.id=:userId and b.res_id=k.module_id) or \n" +
            "                  exists(select * from sys_use_auth a where a.use_id=:userId and a.adjust_type=1 and a.res_id=k.module_id) or \n" +
            "                  exists(select * from sys_resource a where a.id=k.module_id and a.default_val=true ))\n" +
            "             and not exists(select * from sys_use_auth a where a.use_id=:userId and a.adjust_type=2 and a.res_id=k.module_id))")
    List<SysMenu> findUserMenu(@Param("userId") Long userId);
}
